﻿<%@ WebHandler Language="C#" Class="detail" %>
using Models;
using System;
using System.Web;
using Newtonsoft.Json;
using System.Linq;
using System.Collections.Generic;
using Newtonsoft.Json.Linq;
using System.Collections;

public class detail : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";


        string onlyId = context.Request.Headers["X-ACCESS-USER"];//Header中的token
        var xxx=HttpContext.Current.Request.Form["paramJson"];
        JObject jo = (JObject)JsonConvert.DeserializeObject(xxx);
        //{"onlyId":"osiiR5U1hlyCzRDjGqFWWGzE5eqo","taskid":4,"content_text":"eq ","content_images":"","content_video":""}



        string taskid = jo["taskID"].ToString();
        string longitude = jo["longitude"].ToString();
        string latitude = jo["latitude"].ToString();
        string say = jo["say"].ToString();
        string photo = jo["photo"].ToString();
        string type = jo["type"].ToString();


        if (string.IsNullOrEmpty(taskid))
        {
            HttpContext.Current.Response.Write("parameter error");
            return;
        }

        var code = 1;
        var msg = "添加成功";
        var time = DateTime.Now;



        using (Entities bll = new Entities())
        {
            int tid = int.Parse(taskid);

            var xxasa = bll.P_T_Sign.SingleOrDefault(x => x.TaskID == tid && x.UserId == onlyId && x.State ==1);   //vr
            if (xxasa == null)
            {

                var good = bll.S_Goods.SingleOrDefault(x => x.GID == tid);
                var vrjf = good.JF;

                P_T_Sign mytask = new P_T_Sign();

                //新建一个JArray对象，可以理解成json对象中 value 位置的数组对象 ->  []
                mytask.Approval = good.GName;

                mytask.TaskID = tid;
                mytask.addtime = DateTime.Now;
                mytask.UserId = onlyId;
                mytask.Tlongitude = longitude;
                mytask.Tlatitude = latitude;
                mytask.content_text = say;
                mytask.JF =vrjf;
                if (photo != "")
                mytask.photo = photo;
                mytask.type =type;
                mytask.State =1;

                bll.P_T_Sign.Add(mytask);
                bll.SaveChanges();
            }
            else
            {
                if(photo!="")
                    xxasa.photo = photo;


                  xxasa.endTime= DateTime.Now;
                bll.SaveChanges();
                msg =xxasa.addtime+"已经打过卡";

            }
            context.Response.Write(JsonConvert.SerializeObject(new { code, msg, time}));



        }

        HttpContext.Current.Response.End();
        //context.Response.Write("Hello World");


    }





    public bool IsReusable {
        get {
            return false;
        }
    }

}